home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / lang / runtime.jav < prev    next >
Encoding:
Text File  |  1995-10-30  |  7.1 KB  |  245 lines

  1. /*
  2.  * @(#)Runtime.java    1.11 95/09/12 Frank Yellin
  3.  *
  4.  * Copyright (c) 1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.lang;
  21.  
  22. import java.io.*;
  23. import java.util.StringTokenizer;
  24.  
  25.  
  26. public class Runtime {
  27.     private static Runtime currentRuntime = new Runtime();
  28.       
  29.  
  30.     /**
  31.      * Return the runtime.
  32.      */
  33.     public static Runtime getRuntime() { 
  34.     return currentRuntime;
  35.     }
  36.     
  37.     /** Don't let anyone else instantiate this class */
  38.     private Runtime() {}
  39.  
  40.     /* Helper for exit
  41.      */
  42.     private native void exitInternal(int status);
  43.  
  44.     /**
  45.      * Exits the virtual machine with an exit code. This method does
  46.      * not return, use with caution.
  47.      * @param status exit status, 0 if successful, other values indicate
  48.      *        various error types. 
  49.      */
  50.     public void exit(int status) {
  51.     SecurityManager security = System.getSecurityManager();
  52.     if (security != null) {
  53.         security.checkExit(status);
  54.     }
  55.     exitInternal(status);
  56.     }
  57.  
  58.     /* Helper for exec
  59.      */
  60.     private native Process execInternal(String cmdarray[]) 
  61.      throws IOException;
  62.  
  63.     /*
  64.      * Executes the system command specified in the parameter.
  65.      * Returns a Process which has methods for optaining the stdin,
  66.      * stdout, and stderr of the subprocess.  This method fails if
  67.      * executed by untrusted code.
  68.      *
  69.      * @param command a specified system command
  70.      * @return an instance of class Process
  71.      */
  72.     public Process exec(String command) throws IOException {
  73.     int count = 0;
  74.     String cmdarray[];
  75.      StringTokenizer st;
  76.  
  77.     st = new StringTokenizer(command);
  78.      count = st.countTokens();
  79.  
  80.     cmdarray = new String[count];
  81.     st = new StringTokenizer(command);
  82.     count = 0;
  83.      while (st.hasMoreTokens()) {
  84.          cmdarray[count++] = st.nextToken();
  85.      }
  86.     SecurityManager security = System.getSecurityManager();
  87.     if (security != null) {
  88.         security.checkExec(cmdarray[0]);
  89.     }
  90.     return execInternal(cmdarray);
  91.     }
  92.  
  93.     public Process exec(String cmdarray[]) throws IOException {
  94.     SecurityManager security = System.getSecurityManager();
  95.     if (security != null) {
  96.         security.checkExec(cmdarray[0]);
  97.     }
  98.     return execInternal(cmdarray);
  99.     }
  100.  
  101.  
  102.     /**
  103.      * Returns the number of free bytes in system memory. This number
  104.      * is not always accurate because it is just an estimation of the available
  105.      * memory.  More memory may be freed by calling System.gc() .
  106.      */
  107.     public native long freeMemory();
  108.  
  109.     /**
  110.      * Returns the total number of bytes in system memory. 
  111.      */
  112.     public native long totalMemory();
  113.  
  114.     /**
  115.      * Runs the garbage collector.
  116.      */
  117.     public native void gc();
  118.  
  119.     /**
  120.      * Runs the finalization methods of any objects pending finalization.
  121.      * Usually you will not need to call this method since finalization
  122.      * methods will be called asynchronously by the finalization thread.
  123.      * However, under some circumstances (like running out of a finalized
  124.      * resource) it can be useful to run finalization methods synchronously.
  125.      */
  126.     public native void runFinalization();
  127.  
  128.     /**
  129.      * Enables/Disables tracing of instructions.
  130.      * @param on    start tracing if true
  131.      */
  132.     public native void traceInstructions(boolean on);
  133.  
  134.     /**
  135.      * Enables/Disables tracing of method calls.
  136.      * @param on    start tracing if true
  137.      */
  138.     public native void traceMethodCalls(boolean on);
  139.  
  140.     /**
  141.      * Initializes the linker and returns the search path for shared libraries.
  142.      */
  143.     private synchronized native String initializeLinkerInternal();
  144.     private native String buildLibName(String pathname, String filename);
  145.  
  146.     /* Helper for load and loadLibrary */
  147.     private native boolean loadFileInternal(String filename);
  148.  
  149.  
  150.     /** The paths searched for libraries */
  151.     private String paths[];
  152.  
  153.     private void initializeLinker() {
  154.     String ldpath = initializeLinkerInternal();
  155.     char c = System.getProperty("path.separator").charAt(0);
  156.     int ldlen = ldpath.length();
  157.     int i, j, n;
  158.     // Count the separators in the path
  159.     i = ldpath.indexOf(c);
  160.     n = 0;
  161.     while (i >= 0) {
  162.         n++;
  163.         i = ldpath.indexOf(c, i+1);
  164.     }
  165.  
  166.     // allocate the array of paths - n :'s = n + 1 path elements
  167.     paths = new String[n + 1];
  168.  
  169.     // Fill the array with paths from the ldpath
  170.     n = i = 0;
  171.     j = ldpath.indexOf(c);
  172.     while (j >= 0) {
  173.         if (j - i > 0) {
  174.         paths[n++] = ldpath.substring(i, j);
  175.         }
  176.         i = j + 1;
  177.         j = ldpath.indexOf(c, i);
  178.     }
  179.     paths[n] = ldpath.substring(i, ldlen);
  180.     }
  181.     
  182.  
  183.     /**
  184.      * Loads a dynamic library, given a complete path name. If you use this
  185.      * from java_g it will automagically insert "_g" before the ".so".
  186.      *
  187.      * Example: Runtime.getRuntime().load("/home/avh/lib/libX11.so");
  188.      * @param filename the file to load
  189.      * @exception UnsatisfiedLinkError If the file does not exist.
  190.      * @see #getRuntime
  191.      */
  192.     public synchronized void load(String filename) {
  193.     SecurityManager security = System.getSecurityManager();
  194.     if (security != null) {
  195.         security.checkLink(filename);
  196.     }
  197.         if (!loadFileInternal(filename)) {
  198.         throw new UnsatisfiedLinkError(filename);
  199.     }
  200.     }
  201.  
  202.     /**
  203.      * Loads a dynamic library with the specified library name. The 
  204.      * call to LoadLibrary() should be made in the static 
  205.      * initializer of the first class that is loaded. Linking in the 
  206.      * same library more than once is ignored.
  207.      * @param libname the name of the library
  208.      * @exception UnsatisfiedLinkError If the library does not exist. 
  209.      */
  210.     public synchronized void loadLibrary(String libname) {
  211.     SecurityManager security = System.getSecurityManager();
  212.     if (security != null) {
  213.         security.checkLink(libname);
  214.     }
  215.         if (paths == null) {
  216.             initializeLinker();
  217.     }
  218.     for (int i = 0 ; i < paths.length ; i++) {
  219.         String tempname = buildLibName(paths[i], libname);
  220.         if (loadFileInternal(tempname)) {
  221.             return;
  222.         }
  223.     }
  224.     // Oops, it failed
  225.         throw new UnsatisfiedLinkError("no " + libname + 
  226.                        " in shared library path");
  227.     }
  228.  
  229.     /**
  230.      * Localize an input stream. A localized input stream will automatically
  231.      * translate the input from the local format to UNICODE. 
  232.      */
  233.     public InputStream getLocalizedInputStream(InputStream in) {
  234.     return in;
  235.     }
  236.  
  237.     /**
  238.      * Localize an output stream. A localized output stream will automatically
  239.      * translate the output from UNICODE to the local format.
  240.      */
  241.     public OutputStream getLocalizedOutputStream(OutputStream out) {
  242.     return out;
  243.     }
  244. }
  245.